
const { redisSet, redisGet } = require('../libs/redisClient');
const { getCourseData, changeField, changeCourseStatus } = require('../services/Course'),
      { getRecomCourseData, changeRecomCourseStatus  } = require('../services/RecomCourse'),
      { getCourseCollectionData, changeCourseCollectionStatus } = require('../services/CourseCollection'),
      { getTeacherData, changeTeacherStatus, selectStarTeacher } = require('../services/Teacher'),
      { getStudentData, changeStudentStatus } = require('../services/Student'),
      { getSliderData, changeSliderStatus } = require('../services/Slider'),
      { getCourseFieldData } = require('../services/CourseTab'),
      { returnInfo } = require('../libs/utils'),
      { API } = require('../config/error_config');

class IndexController {

  async index (ctx, next) {
    const sess = ctx.session;
    if ( !sess.uid ) {
      sess.uid = 1;
      sess.username = 'jsjiajia';
      sess.nickname = 'js++',
      sess.gender  = 'male';
    }
    ctx.body = {
      session: sess
    }
    // redisSet('name', "xiangyang");\
    // redisSet('json', {a: 1, b:2})
    redisGet('json').then(res => {
      console.log(res);
    })
    // await ctx.render ( 'index');
  }


  /* 获取 数据*/
  async getCourses (ctx, next) {
    const courseData = await getCourseData(), 
          fieldData = await getCourseFieldData();

    ctx.body = courseData && fieldData 
    ? returnInfo(API.RETURN_SUCCESS, {
        courseData,
        fieldData
      })
    : ctx.body = returnInfo(API.RETURN_FAILED);
  }

  async getRecomCourse (ctx, next) {
    const data = await getRecomCourseData();
    ctx.body = data
    ? returnInfo(API.RETURN_SUCCESS, data)
    : ctx.body = returnInfo(API.RETURN_FAILED);
  }

  async getSliderInfo (ctx, next) {
    const data = await getSliderData();
    ctx.body = data
    ? returnInfo(API.RETURN_SUCCESS, data)
    : ctx.body = returnInfo(API.RETURN_FAILED);
  }

  async getCourseCollectionInfo (ctx, next) {
    const data = await getCourseCollectionData();
    ctx.body = data
    ? returnInfo(API.RETURN_SUCCESS, data)
    : ctx.body = returnInfo(API.RETURN_FAILED);
  }

  async getTeachers (ctx, next) {
    const data = await getTeacherData();
    ctx.body = data
    ? returnInfo(API.RETURN_SUCCESS, data)
    : ctx.body = returnInfo(API.RETURN_FAILED);
  }

  async getStudents (ctx, next) {
    const data = await getStudentData();
    ctx.body = data
    ? returnInfo(API.RETURN_SUCCESS, data)
    : ctx.body = returnInfo(API.RETURN_FAILED);
  }



  /* 改变 分类*/
  async changeCourseField (ctx, next) {
    const { cid, field } = ctx.request.body;

    const result = changeField(cid, field);
    if (result) {
      ctx.body = returnInfo(API.SELECT_STAR_TEACHER_SUCCESS);
    }else {
      ctx.body = returnInfo(API.SELECT_STAR_TEACHER_FAILED);
    }
  }

  /* select start teacher*/
  async changeTeacherStar (ctx, next) {
    const { id, isStar } = ctx.request.body;
    console.log(id, isStar)
    const result = await selectStarTeacher(id, isStar);

    if(!result) {
      ctx.body = returnInfo(API.SELECT_STAR_TEACHER_FAILED)
      return;
    }else {
      ctx.body = returnInfo(API.SELECT_STAR_TEACHER_SUCCESS)
      return;
    }
    // selectStarTeacher
  }

  /* 改变上下架状态 抽离公用中间件*/
  async changeDataStatus (ctx, next){
    const { id, status, field } = ctx.request.body;
    let result;

    switch(field) {
      case 'COURSE':
        result = await changeCourseStatus(id, status);
        break;
      case 'RECOM_COURSE':
        result = await changeRecomCourseStatus(id, status);
        break;
      case 'SLIDER':
        result = await changeSliderStatus(id, status);
        break;
      case 'COURSE_COLLECTION':
        result = await changeCourseCollectionStatus(id, status);
        break;
      case 'TEACHER':
        result = await changeTeacherStatus(id, status);
        break;
      case 'STUDENT':
        result = await changeStudentStatus(id, status);
        break;
      default: 
        ctx.body = returnInfo(API.FIELD_ERROR);
        return;
        break;
    }

    if(!result) {
      ctx.body = returnInfo(API.CHANGE_STATUS_FAILED);
    }else {
      ctx.body = returnInfo(API.CHANGE_STATUS_SUCCESS);
    }
  }
}

module.exports = new IndexController();